package com.xxx.yuedong.campaign.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.xxx.yuedong.campaign.controller.form.*;
import com.xxx.yuedong.campaign.pojo.Plan;
import com.xxx.yuedong.campaign.service.PlanService;
import com.xxx.yuedong.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author hc
 * @date 2023/08/23 13:08
 **/

@RestController
@RequestMapping("/plan")
@Api(tags = "计划相关接口")
@Slf4j
public class PlanController {
    @Autowired
    private PlanService planService;

    @PostMapping("/insert")
    @ApiOperation(value = "新增计划(后台管理)")
    public R insert(@Valid @RequestBody InsertPlanForm form){
        log.debug("正在执行 【新增计划】,参数为:{}" + form.toString());
        Plan plan = JSONUtil.parse(form).toBean(Plan.class);
        plan.setCourseId(JSONUtil.parseArray(form.getCourseId()).toString());
        int rows = planService.insert(plan);
        return R.ok().put("rows",rows);
    }

    @PostMapping("/searchPlanByGoal")
    @ApiOperation(value = "根据计划目标查询计划")
    public R searchPlanByGoal(@Valid @RequestBody SearchPlanByGoalForm form){
        log.debug("正在执行 【根据计划目标查询计划】,参数为:{}" + form.toString());
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        ArrayList<HashMap> list = planService.searchPlanByGoal(param);
        return R.ok().put("list",list);
    }

    @PostMapping("/searchPlanById")
    @ApiOperation(value = "根据计划id查询计划")
    public R searchPlanByGoal(@Valid @RequestBody SearchPlanByIdForm form){
        log.debug("正在执行 【根据计划id查询计划】,参数为:{}" + form.toString());
        HashMap param = planService.searchPlanById(form.getId());
        return R.ok().put("param",param);
    }

    @PostMapping("/searchAllPlan")
    @ApiOperation(value = "查询所有计划")
    public R searchAllPlan(){
        ArrayList<HashMap> list = planService.searchAllPlan();
        return R.ok().put("list",list);
    }

    @PostMapping("/searchPlanList")
    @ApiOperation(value = "后台查询所有计划(带分页)")
    public R searchAllPlan(@Valid @RequestBody SearchPlanListForm form){
        PageInfo pageInfo = planService.searchPlanList(form.getPageNum(), form.getPageSize());
        return R.ok().put("page",pageInfo);
    }

    @PostMapping("/deletePlanByIds")
    @ApiOperation(value = "批量删除计划(后台管理)")
    public R deletePlanByIds(@Valid @RequestBody DeletePlanByIdsForm form){
        int rows = planService.deletePlanByIds(form.getIds());
        return R.ok().put("rows",rows);
    }

    @PostMapping("/updatePlanById")
    @ApiOperation(value = "修改计划(后台管理)")
    public R updatePlanById(@Valid @RequestBody UpdatePlanByIdForm form){
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.replace("courseId",JSONUtil.parseArray(form.getCourseId()).toString());
        int rows = planService.updatePlanById(param);
        return R.ok().put("rows",rows);
    }
}
